package cn.dzhang.rcp.hyperbola.common;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import cn.dzhang.rcp.hyperbola.remoting.RemotingConfig;

public class GlobalEnv {
	private static final GlobalEnv instance = new GlobalEnv();
	private AnnotationConfigApplicationContext ctx;

	private GlobalEnv() {
	}

	public static GlobalEnv getInstance() {
		return instance;
	}

	public void refresh() {
		if (ctx != null) {
			ctx.close();
		}

		ctx = new AnnotationConfigApplicationContext();
		ctx.register(RemotingConfig.class);

		ctx.refresh();
	}

	public <T> T getBean(Class<T> clazz) {
		if (ctx == null) {
			refresh();
		}

		return ctx.getBean(clazz);
	}
}
